package mclight;
import flash.display.DisplayObjectContainer;
import flash.events.MouseEvent;
import mclight.containers.Panel;
import mclight.text.Label;
import mclight.containers.Container;

class Window extends Component
{
	public function new(
		?parent:DisplayObjectContainer, ?container:Container, xpos:Float = 0, ypos:Float = 0,
		title:String = "Window"
	)
	{
		_color = -1;
		_shadow = true;
		_title = title;
		super(parent, container, xpos, ypos);
	}
	
	override function init()
	{
		super.init();
		setSize(100, 100);
	}
	
	var _titleBar:Panel;
	
	var _titleLabel:Label;
	
	var _panel:Panel;
	
	override function construct()
	{
		_titleBar = new Panel(_sprite);
		/*var titleBarSprite = _titleBar._sprite;
		titleBarSprite.buttonMode = true;
		titleBarSprite.useHandCursor = true;
		titleBarSprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);*/
		_titleLabel = new Label(_titleBar, 5, 1, _title);
		_panel = new Panel(_sprite, 0, 20);
		_sprite.filters = [getComponentShadow(4, false)];
	}
	
	// PUBLIC METHODS
	
	public override function draw()
	{
		super.draw();
		_titleBar.color = _color;
		_panel.color = _color;
		_titleBar.width = width;
		_panel.setSize(_width, _height - 20);
	}
	
	// HANDLERS
	
	function onMouseDown(event:MouseEvent)
	{
		_sprite.startDrag();
		_sprite.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		_sprite.parent.addChild(_sprite);
	}
	
	function onMouseUp(event:MouseEvent)
	{
		_sprite.stopDrag();
		_sprite.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	}
	
	// PROPERTIES
	
	public var shadow(getShadow, setShadow):Bool;
	var _shadow:Bool;
	function setShadow(b:Bool):Bool
	{
		_shadow = b;
		_titleBar.shadow = _shadow;
		_panel.shadow = _shadow;
		if(_shadow)
		{
			_sprite.filters = [getComponentShadow(4, false)];
		}
		else
		{
			_sprite.filters = null;
		}
		return b;
	}
	function getShadow():Bool
	{
		return _shadow;
	}
	
	public var color(getColor, setColor):Int;
	var _color:Int;
	function setColor(c:Int):Int
	{
		_color = c;
		invalidate();
		return c;
	}
	function getColor():Int
	{
		return _color;
	}
	
	public var title(getTitle, setTitle):String;
	var _title:String;
	function setTitle(value:String):String
	{
		return _titleLabel.text = _title = value;
	}
	function getTitle():String
	{
		return _title;
	}
	
	public var content(getContent, null):DisplayObjectContainer;
	function getContent()
	{
		return null;//TODO To realise
	}
}
